import java.io.*;

public class Utils
{
	public static double magnitude (int [] arr)
	{
		int m = 0;
		for (int i=0; i<arr.length; ++i) m += arr[i]*arr[i];
		return Math.sqrt (m);
	}

	public static double mean (double [] data)
	{	
		double mu=0.0;
		for (int i=0; i<data.length; ++i) mu += data[i];
		return mu/data.length;
	}

	public static double absVariance (double [] data) // for finding \chi_|m|
	{
		double [] temp = new double [data.length];
		for (int i=0; i<data.length; ++i) temp[i] = Math.abs (data[i]);
		return variance (temp);
	}

	public static double variance (double [] data) // algorithm from wikipedia
	{
		double s2=0.0, sc=0.0;
		double mu = mean (data);
		for (int i=0; i<data.length; ++i)
		{
			s2 += (data[i]-mu)*(data[i]-mu);
			sc += data[i]-mu;
		}
		return (s2 - sc*sc/data.length)/(data.length-1);
	}

	public static void arrayOutput (double [] arr, String filename)
	{
		FileOutputStream out;
		PrintStream file;
		try
		{
			out = new FileOutputStream (filename);
			file = new PrintStream (out);
			for (int i=0; i<arr.length; ++i) file.println (arr[i]);
		} catch (IOException e) {
			System.out.println ("File "+filename+" could not open.");
		}
	}

	public static void doubleArrayOutput (double [][] arr, String filename)
	{
		FileOutputStream out;
		PrintStream file;
		try
		{
			out = new FileOutputStream (filename);
			file = new PrintStream (out);
			for (int i=0; i<arr.length; ++i)
			{
				for (int j=0; j<arr[i].length; ++j)
					file.print (arr[i][j]+"\t");
				file.print ("\n");
			}
		} catch (IOException e) {
			System.out.println ("File "+filename+" could not open.");
		}
	}
}

